package com.net.framework.help.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.Spanned;
import android.util.Log;

import com.net.framework.help.R;
import com.net.framework.help.widget.bubbleview.Utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import datetime.DateTime;

/**
 * Data数据处理类
 */
public class DateUtil {

    /**
     * 计算年龄
     *
     * @param
     * @return
     */
    public static int getAge(String strDate) {
        Date dateOfBirth = StringToDate(strDate);
        int age = 0;
        Calendar born = Calendar.getInstance();
        Calendar now = Calendar.getInstance();
        if (dateOfBirth != null) {
            now.setTime(new Date());
            born.setTime(dateOfBirth);
            if (born.after(now)) {
                throw new IllegalArgumentException("还未出生呢");
            }
            age = now.get(Calendar.YEAR) - born.get(Calendar.YEAR);
            if (now.get(Calendar.DAY_OF_YEAR) < born.get(Calendar.DAY_OF_YEAR)) {
                age -= 1;
            }
        }
        return age;
    }

    /**
     * 取出近几年的年份数据列表
     *
     * @param nYears 近多少年
     * @return
     */
    public static List<String> yearsList(int nYears) {
        List<String> years_List = new ArrayList<String>();
        int month = Integer.valueOf(new SimpleDateFormat("yyyy").format(new Date()));// 得到当前年份之前的100年数据
        for (int i = 0; i < nYears; i++) {
            years_List.add((month - i) + "");
        }
        return years_List;
    }

    /***
     * 获取当月和之前的所有月份数据
     */
    public static List<String> monthList() {
        List<String> month_List = new ArrayList<String>();
        int month = Integer.valueOf(new SimpleDateFormat("MM").format(new Date()));// 得到当前月份
        for (int i = month; i >= 1; i--) {
            month_List.add(i + "");
        }
        return month_List;
    }

    /**
     * 获取到当天前一个月的日期数据
     */
    public static void dayTime30List(List<String> dayTime, List<String> dayTimeValue) {
        Calendar c = Calendar.getInstance();
        SimpleDateFormat dFormat = new SimpleDateFormat("MM月dd日");
        for (int i = 0; i < 31; i++) {
            c.setTime(new Date());
            c.add(Calendar.DATE, -i);
            Date date = c.getTime();
            if (i == 0) {
                dayTime.add("今天");
            } else if (i == 1) {
                dayTime.add("昨天");
            } else {
                dayTime.add(dFormat.format(date));
            }
            dayTimeValue.add(getFormatDateTime(date, "yyyy-MM-dd"));
        }
    }


    /**
     * 获取到当月前12个月的月份数据
     */
    public static void monthTime12List(List<String> monthTime, List<String> monthTimeValue) {
        Calendar c = Calendar.getInstance();
        SimpleDateFormat mFormat = new SimpleDateFormat("MM月");
        SimpleDateFormat ymFormat = new SimpleDateFormat("yyyy年MM月");
        //记录当前的年份
        int theYear = c.getTime().getYear();
        for (int i = 0; i < 13; i++) {
            c.setTime(new Date());
            c.add(Calendar.MONTH, -i);
            Date date = c.getTime();
            if (i == 0) {
                monthTime.add("当月");
            } else if (i == 1) {
                monthTime.add("上一个月");
            } else {
                if (theYear == c.getTime().getYear()) {
                    monthTime.add(mFormat.format(date));
                } else {
                    monthTime.add(ymFormat.format(date));
                }
            }
            monthTimeValue.add(getFormatDateTime(date, "yyyy-MM-dd"));
        }
    }


    /**
     * 根据时间段，得出当前时间是 白天还是晚上
     *
     * @param begTime begTime (08:00)
     * @param endTime endTime (20:00)
     * @return 白天/黑夜
     */
    public static String getIsNight(String begTime, String endTime) {
        String isNight = "";

        String nowTime = getCurrentDateTime();

        String begdate = getCurrentDate() + " " + begTime + ":00";
        long i = DateUtil.getDiff(nowTime, begdate);

        String dateEnd = getCurrentDate() + " " + endTime + ":00";
        long j = DateUtil.getDiff(nowTime, dateEnd);

        if (i <= 0 && j >= 0) {
            isNight = "day";
        } else {
            isNight = "night";
        }
        return isNight;
    }

    /**
     * 返回当前时间字符串。 格式：yyyy-MM-dd
     *
     * @return String 指定格式的日期字符串.
     */
    public static String getCurrentDate() {
        return getFormatDateTime(new Date(), "yyyy-MM-dd");
    }

    /***
     * 取时间字符串。格式：HH:mm
     *
     * @param date
     * @return
     */
    public static String getHourMinute(Date date) {
        String path1 = "HH:mm";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(path1, Locale.ENGLISH);
        String str = simpleDateFormat.format(date);// 从给定的字符串中提取出来日期
        return str;
    }

    /**
     * 返回当前指定的时间戳。格式为yyyy-MM-dd HH:mm:ss
     *
     * @return 格式为yyyy-MM-dd HH:mm:ss，总共19位。
     */
    public static String getCurrentDateTime() {
        return getFormatDateTime(new Date(), "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 返回当前指定的时间戳。格式为yyyy-MM-dd HH:mm
     *
     * @return 格式为yyyy-MM-dd HH:mm。
     */
    public static String getCurrentYMDHMDateTime() {
        return getFormatDateTime(new Date(), "yyyy-MM-dd HH:mm:ss");
    }


    /**
     * 根据给定的格式与时间(Date类型的)，返回时间字符串。最为通用。<br>
     *
     * @param date   指定的日期
     * @param format 日期格式字符串
     * @return String 指定格式的日期字符串.
     */
    public static String getFormatDateTime(Date date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }


    public static String getMMDDHHmm(String time) {
        if (StringUtil.isBlank(time)) {
            return "未知";
        }
        String dtstr = "";
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date datetime = ft.parse(time);
            dtstr += (datetime.getMonth() + 1) + "-";
            dtstr += datetime.getDate() + " ";
            String hour = datetime.getHours() + "";
            String minute = datetime.getMinutes() + "";
            dtstr += hour + ":" + minute;
        } catch (Exception e) {
        }
        return dtstr;
    }

    public static String getFormatDateTime(String time, String format) {
        if (StringUtil.isBlank(time)) {
            return "未知";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date d = null;
        try {
            d = sdf.parse(time); // 将给定的字符串中的日期提取出来
        } catch (Exception e) { // 如果提供的字符串格式有错误，则进行异常处理
            e.printStackTrace(); // 打印异常信息
        }
        return sdf.format(d);
    }

    /**
     * 给指定的时间字符串格式化，返回时间格式为 yyyy-MM-dd HH:mm:ss 的时间串
     *
     * @param time 原时间串
     * @return String 格式化后的字符串
     */
    public static String getFormatDateString(String time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d = null;
        try {
            d = sdf.parse(time); // 将给定的字符串中的日期提取出来
        } catch (Exception e) { // 如果提供的字符串格式有错误，则进行异常处理
            e.printStackTrace(); // 打印异常信息
        }
        return sdf.format(d);
    }

    /**
     * 给指定的时间字符串格式化，返回时间格式为 yyyy-MM-dd HH:mm 的时间串
     *
     * @param time 原时间串
     * @return String 格式化后的字符串
     */
    public static String getFormatDateHMString(String time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date d = null;
        try {
            d = sdf.parse(time); // 将给定的字符串中的日期提取出来
        } catch (Exception e) { // 如果提供的字符串格式有错误，则进行异常处理
            e.printStackTrace(); // 打印异常信息
        }
        return sdf.format(d);
    }

    /**
     * 给指定的时间字符串格式化，返回时间格式为 yyyy-MM-dd的时间串
     *
     * @param time 原时间串
     * @return String 格式化后的字符串
     */
    public static String getFormatDateYMDString(String time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date d = null;
        try {
            d = sdf.parse(time); // 将给定的字符串中的日期提取出来
        } catch (Exception e) { // 如果提供的字符串格式有错误，则进行异常处理
            e.printStackTrace(); // 打印异常信息
        }
        return sdf.format(d);
    }

    public static String getFormatDateYMDDianString(String time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date d = null;
        try {
            d = sdf.parse(time); // 将给定的字符串中的日期提取出来
        } catch (Exception e) { // 如果提供的字符串格式有错误，则进行异常处理
            e.printStackTrace(); // 打印异常信息
        }
        return sdf.format(d);
    }

    /****
     * 给指定的时间字符串格式化，返回指定格式的时间串
     *
     * @param time      时间串
     * @param thereTime 时间串格式
     * @return
     */
    public static String getFormatDateYMDHMString(String time, String thereTime) {
        SimpleDateFormat sdf = new SimpleDateFormat(thereTime);
        Date d = null;
        try {
            d = sdf.parse(time); // 将给定的字符串中的日期提取出来
        } catch (Exception e) { // 如果提供的字符串格式有错误，则进行异常处理
            e.printStackTrace(); // 打印异常信息
        }
        return sdf.format(d);
    }

    public static String getChinaDateYMDString(String datetimeString) {
        String dtstr = "";
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date datetime = ft.parse(datetimeString);
            dtstr += (datetime.getYear() + 1900) + "年";
            dtstr += (datetime.getMonth() + 1) + "月";
            dtstr += datetime.getDate() + "日 ";
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return dtstr;
    }

    /**
     * 获取两个时间串时间的差值，单位为秒
     *
     * @param startTime 开始时间 yyyy-MM-dd HH:mm:ss
     * @param endTime   结束时间 yyyy-MM-dd HH:mm:ss
     * @return 两个时间的差值(秒)
     */
    public static long getDiff(String startTime, String endTime) {
        long diff = 0;
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date startDate = ft.parse(startTime);
            Date endDate = ft.parse(endTime);
            diff = endDate.getTime() - startDate.getTime();
            diff = diff / 1000;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return diff;
    }

    /**
     * 获取两个时间串时间的差值，单位为秒
     *
     * @param startDate 开始时间 yyyy-MM-dd HH:mm:ss
     * @param endDate   结束时间 yyyy-MM-dd HH:mm:ss
     * @return 两个时间的差值(秒)
     */
    public static long getDiffByDate(Date startDate, Date endDate) {
        long diff = 0;
        try {
            diff = endDate.getTime() - startDate.getTime();
            diff = diff / 1000;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return diff;
    }

    /**
     * 获取传入时间与当前时间的差值，单位为秒
     *
     * @param lastDate 结束时间 yyyy-MM-dd HH:mm:ss
     * @return 两个时间的差值(秒)
     */
    public static long getDiffByDate(Date lastDate) {
        long diff = 10;
        try {
            Date endDate = new Date(System.currentTimeMillis());
            diff = endDate.getTime() - lastDate.getTime();
            diff = diff / 1000;
            // Log.d("","FLAG  diff time="+diff);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return diff;
    }

    /***
     * 比较两个时间的大小
     *
     * @param startTime 起始时间
     * @param endTime   结束时间
     * @return 返回秒的差值
     */
    public static long getDiffForYMDHM(String startTime, String endTime) {
        long diff = 0;
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date startDate = ft.parse(startTime);
            Date endDate = ft.parse(endTime);
            diff = endDate.getTime() - startDate.getTime();
            diff = diff / 1000;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return diff;
    }

    public static long getDiffForYMDHMSS(Date baseTime, Date noticeTime) {
        long diff = 0;
        try {
            diff = noticeTime.getTime() - baseTime.getTime();
            diff = diff / 1000;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return diff;
    }

    /**
     * 获取当前日期的后一天
     *
     * @return 当前日期+1
     */

    public static Date nextDate() {

        Calendar cal = Calendar.getInstance();// 使用默认时区和语言环境获得一个日历。

        cal.add(Calendar.DAY_OF_MONTH, +1);// 取当前日期的后一天.

        return cal.getTime();
    }

    public static Date nextDate(String startdate) {
        Date date = null;
        try {
            date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(startdate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, 1);
        return cal.getTime();
    }


    /**
     * 得到前天的日期（N月N日）
     *
     * @return
     */
    public static String anteayerDate() {
        Calendar cal = Calendar.getInstance();// 使用默认时区和语言环境获得一个日历。
        cal.add(Calendar.DAY_OF_MONTH, -2);// 取当前日期的后一天.
        return getChinaDateStringByDateStringNOYearNOTime(cal.getTime());
    }

    /***
     * 获取离当前N个小时之后的时间
     *
     * @return
     */
    public static Date nextTime(int hour) {
        Calendar cal = Calendar.getInstance();// 使用默认时区和语言环境获得一个日历。

        cal.add(Calendar.HOUR, +hour);// 取当前时间再加上N个小时后的时间

        return cal.getTime();

    }

    /**
     * 当前N分钟之后的时间
     */
    public static Date nextMinuteTime(int minute) {
        Calendar cal = Calendar.getInstance();// 使用默认时区和语言环境获得一个日历。
        cal.add(Calendar.MINUTE, +minute);// 取当前时间再加上N个分钟后的时间
        return cal.getTime();
    }

    /**
     * 得到传入日期的后一天
     *
     * @param beginDate
     * @param addDay
     * @return
     * @throws Exception
     */
    @SuppressLint("SimpleDateFormat")
    public static Date getTheNextDay(String beginDate, int addDay) {
        SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd");
        long time = 0;
        try {
            Date d1 = sim.parse(beginDate.trim());
            time = d1.getTime();
            addDay = addDay * 24 * 60 * 60 * 1000;
            time += addDay;
            return new Date(time);
        } catch (Exception e) {
            e.printStackTrace();

        }
        return null;
    }

    /**
     * 获取两个日期之前的天数
     *
     * @param beginDate
     * @param endDate
     * @return
     * @throws Exception
     */
    @SuppressLint("SimpleDateFormat")
    public static int getBetweenDay(String beginDate, String endDate) {
        SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date d1 = sim.parse(beginDate);
            Date d2 = sim.parse(endDate);
            return (int) ((d2.getTime() - d1.getTime()) / (3600L * 1000 * 24));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 将字符串转化成 日期
     *
     * @param dateStr
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    public static Date StringToDate(String dateStr) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = sdf.parse(dateStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 将字符串转化成 日期-时间
     *
     * @param dateStr
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    public static Date StringToDatetime(String dateStr) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date date = null;
        try {
            date = sdf.parse(dateStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 获取中文格式时间 2012年12月12日 上午3:30
     *
     * @param datetimeString
     * @return
     */

    public static String getChinaDateStringByDateString(String datetimeString) {
        String dtstr = "";
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date datetime = ft.parse(datetimeString);
            dtstr += (datetime.getYear() + 1900) + "年";
            dtstr += (datetime.getMonth() + 1) + "月";
            dtstr += datetime.getDate() + "日 ";
            int hour = datetime.getHours();
            String timeString = "";
            if (hour < 12) {// 上午
                timeString = "上午" + hour;
            } else if (hour == 12) { // 中午
                timeString = "中午" + hour;
            } else {// 下午
                timeString = "下午" + (hour - 12);
            }
            int minute = datetime.getMinutes();
            String minuteStr = "";
            if (minute < 10) {
                minuteStr = "0" + minute;
            } else {
                minuteStr = minute + "";
            }
            dtstr += timeString + ":" + minuteStr;
        } catch (Exception e) {
        }
        return dtstr;
    }

    /**
     * 获取中文格式时间 2012年12月12日 上午3:30
     *
     * @param datetimeString
     * @return
     */
    public static String getChinaDateStringBy24DateString(String datetimeString) {
        String dtstr = "";
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date datetime = ft.parse(datetimeString);
            dtstr += (datetime.getYear() + 1900) + "年";
            dtstr += (datetime.getMonth() + 1) + "月";
            dtstr += datetime.getDate() + "日 ";
            int hour = datetime.getHours();
            int minute = datetime.getMinutes();
            String minuteStr = "";
            String hourStr = "";
            if (hour < 10) {
                hourStr = "0" + hour;
            } else {
                hourStr = hour + "";
            }

            if (minute < 10) {
                minuteStr = "0" + minute;
            } else {
                minuteStr = minute + "";
            }
            dtstr += hourStr + ":" + minuteStr;
        } catch (Exception e) {
        }
        return dtstr;
    }

    /**
     * 获取中文格式时间 12月12日 13:30
     *
     * @param datetimeString
     * @return
     */
    public static String getChinaDateStringByDateStringNOYear(String datetimeString) {
        String dtstr = "";
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date datetime = ft.parse(datetimeString);

            dtstr += (datetime.getMonth() + 1) + "月";
            dtstr += datetime.getDate() + "日 ";
            String hour = datetime.getHours() + "";
            int minute = datetime.getMinutes();
            String minuteStr = "";
            if (minute < 10) {
                minuteStr = "0" + minute;
            } else {
                minuteStr = minute + "";
            }
            dtstr += hour + ":" + minuteStr;
        } catch (Exception e) {
        }
        return dtstr;
    }

    /**
     * 返回样式：  00月00日 今天
     *
     * @param lDate
     * @return
     */
    public static String getChinaDateStringByDateDay(long lDate) {
        Date date = new Date(lDate);
        String dtstr = "";
        try {
            dtstr += (date.getMonth() + 1) + "月";
            dtstr += date.getDate() + "日";
            dtstr += " 今天";
        } catch (Exception e) {
        }
        return dtstr;
    }

    /**
     * 返回样式：  00点 00分
     *
     * @param lDate
     * @return
     */
    public static String getChinaDateStringByTime(long lDate) {
//        SimpleDateFormat sdf = new SimpleDateFormat("HH点 mm分");
        Date datetime = new Date(lDate);
        String dtstr = "";
        int hour = datetime.getHours();
        String hourString;
        if (hour < 10) {
            hourString = "0" + hour;
        } else {// 下午
            hourString = hour + "";
        }
        int minute = datetime.getMinutes();
        String minuteStr;
        if (minute < 10) {
            minuteStr = "0" + minute;
        } else {
            minuteStr = minute + "";
        }
        dtstr += hourString + "点 " + minuteStr + "分";
        return dtstr;
    }


    /**
     * 获取中文格式时间 12-12 13:30
     *
     * @param datetimeString
     * @return
     */
    public static String getChinaDateStringByDateStringNOYears(String datetimeString) {
        String dtstr = "";
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date datetime = ft.parse(datetimeString);
            dtstr += (datetime.getMonth() + 1) + "-";
            dtstr += datetime.getDate() + " ";
            String hour = datetime.getHours() + "";
            int minute = datetime.getMinutes();
            String minuteStr = "";
            if (minute < 10) {
                minuteStr = "0" + minute;
            } else {
                minuteStr = minute + "";
            }
            dtstr += hour + ":" + minuteStr;
        } catch (Exception e) {
        }
        return dtstr;
    }

    /**
     * 获取中文格式时间 12月12日
     *
     * @param datetimeString
     * @return
     */
    public static String getChinaDateStringByDateStringNOYearNOTime(String datetimeString) {
        String dtstr = "";
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date datetime = ft.parse(datetimeString);

            dtstr += (datetime.getMonth() + 1) + "月";
            dtstr += datetime.getDate() + "日 ";
        } catch (Exception e) {
        }
        return dtstr;
    }

    /**
     * 获取中文格式时间 12月12日
     *
     * @param datetime
     * @return
     */
    public static String getChinaDateStringByDateStringNOYearNOTime(Date datetime) {
        String dtstr = "";
        try {
            dtstr += (datetime.getMonth() + 1) + "月";
            dtstr += datetime.getDate() + "日 ";
        } catch (Exception e) {
        }
        return dtstr;
    }

    /**
     * 日期比较 （同天返回0，同月 1 同年返回2 ，跨年返回3）
     *
     * @param date1
     * @param date2
     * @return
     */
    public static boolean isSameDate(Date date1, Date date2) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date1);
        int year1 = calendar.get(Calendar.YEAR);
        int month1 = calendar.get(Calendar.MONTH);
        int day1 = calendar.get(Calendar.DAY_OF_YEAR);

        calendar.setTime(date2);
        int year2 = calendar.get(Calendar.YEAR);
        int month2 = calendar.get(Calendar.MONTH);
        int day2 = calendar.get(Calendar.DAY_OF_YEAR);

        if (year1 == year2) {
            if (month1 == month2) {
                if (day1 == day2) {
                    return true;
                }
                // return 1;
            }
            // return 2;
        }
        // return 3;
        return false;
    }

    /***
     * 如果是同一天，则直接显示今天（今天 12：00）；否则显示明确日期（2015-02-02 12：00）
     *
     * @return
     */
    public static String getTimeString(String time) {
        String dtstr = "";
        if (!StringUtil.isBlank(time)) {
            if (isSameDate(StringToDate(getCurrentDate()), StringToDate(time))) {// 同一天
                String Str_time = getFormatDateYMDHMString(time, "yyyy-MM-dd HH:mm");
                String[] stringDateTime = Str_time.split(" ");
                dtstr = "今天  " + stringDateTime[1];
            } else {// 非统同一天
                dtstr = getFormatDateHMString(time);
            }
        }
        return dtstr;
    }

    /***
     * 计算是否在60秒之内
     *
     * @param nowDatetimeString  当前的时间
     * @param lastDatetimeString 最后保存的时间
     * @return true 还在60秒时间范围之内
     */
    public static boolean scope60Time(String nowDatetimeString, String lastDatetimeString) {
        if (lastDatetimeString.equals("0")) {
            return false;
        } else {
            long diff = Math.abs(getDiffForYMDHM(nowDatetimeString, lastDatetimeString));// 相差多少秒
            System.out.println("时间差===" + diff + "秒");
            if (diff <= 60) {// 在60秒之类
                return true;
            } else {
                return false;
            }
        }

    }

    /**
     * 计算时间差（属于一小时内、一小时外）
     *
     * @param nowDatetimeString  当前时间
     * @param lastDatetimeString 上一次被记的录时间
     * @return
     */
    public static String updateTime(String nowDatetimeString, String lastDatetimeString) {
        String update = null;
        long diff = Math.abs(getDiffForYMDHM(nowDatetimeString, lastDatetimeString) / 60);// 相差多少分钟
        if (diff <= 1) {
            update = "刚刚";
        } else if (diff <= 59) {
            update = diff + "分钟前";
        } else if (diff > 59 && diff <= 119) {
            update = "1小时前";
        } else if (diff > 119 && diff <= 179) {
            update = "2小时前";
        } else if (diff > 179 && diff <= 239) {
            update = "3小时前";
        } else if (diff > 1439 && diff <= 2879) {// 一天前
            update = "1天前";
        } else {
            Calendar calendar = Calendar.getInstance();
            // 当前时间
            Date now_data = StringToDatetime(nowDatetimeString);
            calendar.setTime(now_data);
            int now_day = calendar.get(Calendar.DAY_OF_YEAR);
            // 上一次记录的时间
            Date last_data = StringToDatetime(lastDatetimeString);
            calendar.setTime(last_data);
            int last_day = calendar.get(Calendar.DAY_OF_YEAR);
            if (now_day == last_day) {// 同一天
                update = "今天" + getHourMinute(last_data);
            } else {// 不是同一天
                if (diff < 1439) {
                    update = "昨天" + getHourMinute(last_data);
                } else {
                    update = lastDatetimeString;
                }
            }
        }
        return update;
    }

    /**
     * 根据日期获得所在周的日期
     *
     * @param mdate
     * @return
     */
    public static List<Date> dateToWeekMon(Date mdate) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd EEE");

        int b = mdate.getDay();
        Date fdate;
        List<Date> list = new ArrayList<Date>();
        Long fTime = mdate.getTime() - b * 24 * 3600000;
        for (int a = 1; a <= 7; a++) {
            fdate = new Date();
            fdate.setTime(fTime + (a * 24 * 3600000));
            list.add(a - 1, fdate);
        }

        return list;
    }

    /**
     * 根据日期获得所在周的日期
     *
     * @param mdate
     * @return
     */
    public static List<Date> dateToWeekSun(Date mdate) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd EEE");

        int b = mdate.getDay();
        Date fdate;
        List<Date> list = new ArrayList<Date>();
        Long fTime = mdate.getTime() - b * 24 * 3600000;
        for (int a = 0; a <= 6; a++) {
            fdate = new Date();
            fdate.setTime(fTime + (a * 24 * 3600000));
            list.add(a, fdate);
        }
        //
        // System.out.println("今天的日期: " + sdf.format(mdate));
        // for (Date date : list) {
        // System.out.println(sdf.format(date));
        // }

        return list;
    }

    /***
     * 根据不同的时间段返回上午好、下午好、晚上好、凌晨好
     *
     * @return 返回提示语字符串
     */
    public static String welcomeHint(Context context) {
        int hour = Integer.valueOf(new SimpleDateFormat("HH").format(new Date()));// 得到当前时间的整点部分
        String appName = context.getResources().getString(R.string.app_name);
        if (hour < 6) {
            return "凌晨好，欢迎来到" + appName;
        }
        if (hour >= 6 && hour < 12) {
            return "上午好，欢迎来到" + appName;
        }
        if (hour >= 12 && hour < 18) {
            return "下午好，欢迎来到" + appName;
        }
        if (hour >= 18) {
            return "晚上好，欢迎来到" + appName;
        }
        return "您好，欢迎来到" + appName;
    }


    public static long getDateLongTime(String DatetimeString) {
        if (StringUtil.isBlank(DatetimeString)) {
            return 0;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d = null;
        try {
            d = sdf.parse(DatetimeString);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } // 将给定的字符串中的日期提取出来
        return d.getTime();
    }

    public static String transferLongToDateyyyyMMdd(long lDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date(lDate);
        return sdf.format(date);
    }

    public static String transferLongToDateMMddEEE(long lDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日 EEE");
        Date date = new Date(lDate);
        return sdf.format(date);
    }

    public static String transferLongToDate(long lDate) {
        if (lDate == 0) {
            return "--";
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(lDate);
        return sdf.format(date);
    }


    public static String transferLongMMddToDate(long lDate) {
        if (lDate == 0) {
            return "--";
        }
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");
        Date date = new Date(lDate);
        return sdf.format(date);
    }

    public static String transferLongyyyyMMddHHmmToDate(long lDate) {
        if (lDate == 0) {
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date date = new Date(lDate);
        return sdf.format(date);
    }

    public static String transferFormatChinaDate(String dateStr) {
        if (StringUtil.isBlank(dateStr)) {
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String name = "";
        try {
            Date d = sdf.parse(dateStr);
            name += getDayString(d);
            int hour = d.getHours();
            String hourString;
            if (hour < 10) {
                hourString = "0" + hour;
            } else {// 下午
                hourString = hour + "";
            }
            name += hourString + ":";
            int minute = d.getMinutes();
            String minuteStr;
            if (minute < 10) {
                minuteStr = "0" + minute;
            } else {
                minuteStr = minute + "";
            }
            name += minuteStr;
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return name;
    }

    public static String getDayString(Date startDate) {
        int offSet = Calendar.getInstance().getTimeZone().getRawOffset();
        long today = (System.currentTimeMillis() + offSet) / 86400000;
        long start = (startDate.getTime() + offSet) / 86400000;
        int d = (int) (start - today);
        String strName = "";
        //0：今天   1：明天   2：后天
        if (d == 0) {
            strName = "今天";
        } else if (d == 1) {
            strName = "明天";
        } else if (d == 2) {
            strName = "后天";
        } else {
            strName += (startDate.getMonth() + 1) + "月";
            strName += startDate.getDate() + "日";
        }
        return strName;
    }

    /***
     * 得到当前日期之后30天的日期数据
     *
     * @param
     * @return
     */
    public static List<String> dataAfter100Day() {
        List<String> list = new ArrayList<String>();
        Calendar c = Calendar.getInstance();
        SimpleDateFormat dFormat = new SimpleDateFormat("MM月dd日");
        SimpleDateFormat todayFormat = new SimpleDateFormat("今天");
        SimpleDateFormat tomorrowFormat = new SimpleDateFormat("明天");
        for (int i = 0; i < 365; i++) {
            c.setTime(new Date());
            c.add(Calendar.DATE, i);
            Date d2 = c.getTime();
            String s;
            if (i == 0) {
                s = todayFormat.format(d2);
            } else if (i == 1) {
                s = tomorrowFormat.format(d2);
            } else {
                s = dFormat.format(d2);
            }
            list.add(i, s);
        }
        return list;
    }

    /***
     * 得到当前日期之后7天的日期数据
     *
     * @param
     * @return
     */
    public static List<String> dataAfter7Day() {
        List<String> list = new ArrayList<String>();
        Calendar c = Calendar.getInstance();
        SimpleDateFormat dFormat = new SimpleDateFormat("MM月dd日 EEE");
        SimpleDateFormat todayFormat = new SimpleDateFormat("今天 EEE");
        SimpleDateFormat tomorrowFormat = new SimpleDateFormat("明天 EEE");
        for (int i = 0; i < 7; i++) {
            c.setTime(new Date());
            c.add(Calendar.DATE, i);
            Date d2 = c.getTime();
            String s;
            if (i == 0) {
                s = todayFormat.format(d2);
            } else if (i == 1) {
                s = tomorrowFormat.format(d2);
            } else {
                s = dFormat.format(d2);
            }
            list.add(i, s);
        }
        return list;
    }

    /**
     * 计算时间差，返回形式为：XX天XX小时XX分XX秒
     */
    public static String getDistanceTime(String time1, String time2) {
        SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String timeDifference = null;
        try {
            Date begin = dfs.parse(time1);
            Date end = dfs.parse(time2);
            long between = (end.getTime() > begin.getTime() ? end.getTime() - begin.getTime() : begin.getTime() - end.getTime()) / 1000;//除以1000是为了转换成秒
            long day1 = between / (24 * 3600);
            long hour1 = between % (24 * 3600) / 3600;
            long minute1 = between % 3600 / 60;
            if (day1 <= 0 && hour1 <= 0 && minute1 <= 0) {
                timeDifference = "刚刚";
            } else {
                timeDifference = (day1 != 0 ? day1 + "天" : "") + (hour1 != 0 ? hour1 + "小时" : "") + (minute1 != 0 ? minute1 + "分钟" : "");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return timeDifference;
    }


    public static DateTime getAvailableDateTime() {
        DateTime availableTime = new DateTime();
        availableTime.addMinute(10);
        int minute = availableTime.getMinute();
//        minute = (minute + 9) / 10 * 10;
        availableTime.setMinute(minute);
        return availableTime;
    }

    /**
     * 格式化时间展示为05:10
     */
    public static String formatRecordTime(long recTime) {
        int time = (int) (recTime / 1000);
        return formatSTime(time);
    }

    public static String formatSTime(int s) {
        int minute = s / 60;
        int second = s % 60;
        return String.format("%2d:%02d", minute, second);
    }


    public static String formatTime(int recTime) {
        int minute = recTime / 60;
        int second = recTime % 60;
        if (minute > 0) {
            return String.format("%2d’%02d”", minute, second);
        } else {
            return String.format("%2d”", second);
        }
    }

    /**
     * 计算两个日期之间的日期
     *
     * @param startTime
     * @param endTime
     */
    public static void betweenDays(long startTime, long endTime) {
        long ONE_DAY_MS = 24 * 60 * 60 * 1000;
        Date date_start = new Date(startTime);
        Date date_end = new Date(endTime);
        //计算日期从开始时间于结束时间的0时计算
        Calendar fromCalendar = Calendar.getInstance();
        fromCalendar.setTime(date_start);
        fromCalendar.set(Calendar.HOUR_OF_DAY, 0);
        fromCalendar.set(Calendar.MINUTE, 0);
        fromCalendar.set(Calendar.SECOND, 0);
        fromCalendar.set(Calendar.MILLISECOND, 0);
        Calendar toCalendar = Calendar.getInstance();
        toCalendar.setTime(date_end);
        toCalendar.set(Calendar.HOUR_OF_DAY, 0);
        toCalendar.set(Calendar.MINUTE, 0);
        toCalendar.set(Calendar.SECOND, 0);
        toCalendar.set(Calendar.MILLISECOND, 0);
        int s = (int) ((toCalendar.getTimeInMillis() - fromCalendar.getTimeInMillis()) / (ONE_DAY_MS));
        if (s > 0) {
            for (int i = 0; i <= s; i++) {
                long todayDate = fromCalendar.getTimeInMillis() + i * ONE_DAY_MS;
                if (i == 0) {
                    LogUtils.println("打印日期", getChinaDateStringByDateDay(todayDate));
                } else {
                    LogUtils.println("打印日期", transferLongToDateMMddEEE(todayDate));
                }
            }
        } else {//此时在同一天之内
            LogUtils.println("打印日期", getChinaDateStringByDateDay(startTime));
        }
    }


    /**
     * 将秒单位的数据转化为时、分（分钟值向上取整）
     *
     * @param second
     * @return
     */
    public static String formatHMS(int second) {
        int h = 0;
        int m = 0;
        int s = 0;
        int temp = second % 3600;
        if (second > 3600) {
            h = second / 3600;
            if (temp != 0) {
                if (temp > 60) {
                    m = temp / 60;
                    if (temp % 60 != 0) {
                        s = temp % 60;
                    }
                } else {
                    s = temp;
                }
            }
        } else {
            m = second / 60;
            if (second % 60 != 0) {
                s = second % 60;
            }
        }
        String hStr = "";
        if (h > 0) {
            hStr = h + "小时";
        }
        String mStr;
        //如果秒钟大于0，则向上取整数
        if (s > 0) {
            m++;
        }
        if (m > 9) {
            mStr = m + "";
        } else {
            if (h > 0) {
                mStr = "0" + m;
            } else {
                mStr = "" + m;
            }
        }
        return hStr + mStr + "分钟";
    }


    public static Spanned formatHMSBigStrong(int second) {
        int h = 0;
        int m = 0;
        int s = 0;
        int temp = second % 3600;
        if (second > 3600) {
            h = second / 3600;
            if (temp != 0) {
                if (temp > 60) {
                    m = temp / 60;
                    if (temp % 60 != 0) {
                        s = temp % 60;
                    }
                } else {
                    s = temp;
                }
            }
        } else {
            m = second / 60;
            if (second % 60 != 0) {
                s = second % 60;
            }
        }
        String hStr = "";
        if (h > 0) {
            hStr = h + "";
        }
        String mStr;
        //如果秒钟大于0，则向上取整数
        if (s > 0) {
            m++;
        }
        if (m > 9) {
            mStr = m + "";
        } else {
            if (h > 0) {
                mStr = "0" + m;
            } else {
                mStr = "" + m;
            }
        }
//        hStr + mStr + "分钟";
        Spanned spanned = null;
        if (StringUtil.isBlank(hStr)) {
            spanned = KCStringUtils.getHtmlBlackBigStrongSpanned(R.string.times_m_text, mStr);
        } else {
            spanned = KCStringUtils.getHtmlBlackBigStrongSpanned(R.string.times_h_m_text, hStr, mStr);
        }
        return spanned;
    }
}
